#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(this->width(),this->height());
    move((qApp->desktop()->availableGeometry().width() - width()) / 2 + qApp->desktop()->availableGeometry().x(), (qApp->desktop()->availableGeometry().height() - height()) / 2 + qApp->desktop()->availableGeometry().y());
    ui->textBrowser->hide();
    ui->pushButton_2->hide();

    process = new QProcess;
    process->setProcessChannelMode(QProcess::MergedChannels);
    connect(process, &QProcess::readyReadStandardOutput, [&]
    {
        ui->textBrowser->append(process->readAllStandardOutput());
    });

    system("rm -rf /tmp/spark-store-installer");
    system("mkdir -p /tmp/spark-store-installer");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    if(ui->lineEdit->text().isEmpty())
    {
        QMessageBox::StandardButton result = QMessageBox::information(NULL, "提示", "下载链接为空，是否跳转到最新版本下载地址？\n（需要手动复制下载地址）", QMessageBox::Yes | QMessageBox::No);
        switch (result)
        {
            case QMessageBox::Yes:
            process->start("xdg-open " + downloadlink);
            process->waitForFinished();
            process->close();
            break;
            case QMessageBox::No:
            break;
            default:
            break;
        }
    }
    else {
        downloadlink = ui->lineEdit->text();
        if(!((downloadlink.startsWith("http") || downloadlink.startsWith("https") || downloadlink.startsWith("ftp")) && downloadlink.endsWith(".deb")))
        {
            QMessageBox::warning(NULL, "警告", "请输入合法下载链接！");
            ui->lineEdit->clear();
            return;
        }
        osrelease = ui->comboBox->currentText();
        if(osrelease == "UOS")
        {
            downloadlink2 = "http://app-store.githall.com/spark-store-dependencies-uos.zip";
        }
        else if(osrelease == "Ubuntu / Debian")
        {
            downloadlink2 = "http://app-store.githall.com/spark-store-dependencies-kylin.zip";
        }
        process->start("wget -O /tmp/spark-store-installer/spark-store.deb " + downloadlink);
        process->waitForFinished();
        process->close();
        if(!downloadlink2.isEmpty())
        {
            process->start("wget -P /tmp/spark-store-installer " + downloadlink2);
            process->waitForFinished();
            process->close();
            system("unzip -d /tmp/spark-store-installer /tmp/spark-store-installer/*.zip");
        }
        QMessageBox::StandardButton result = QMessageBox::information(NULL, "提示", "所有组件下载完成,是否继续安装？", QMessageBox::Yes | QMessageBox::No);
        switch (result)
        {
            case QMessageBox::Yes:
            ui->textBrowser->show();
            ui->pushButton_2->show();
            install_all_debs();
            break;
            case QMessageBox::No:
            return;
            default:
            return;
        }
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->textBrowser->hide();
    ui->pushButton_2->hide();
}

void MainWindow::install_all_debs()
{
    if(!downloadlink2.isEmpty())
        system("pkexec apt install /tmp/spark-store-installer/spark-store-dependencies/*.deb");
    process->start("pkexec apt install /tmp/spark-store-installer/spark-store.deb");
    process->waitForFinished();
    process->close();
    QMessageBox::information(NULL, "提示", "星火商店安装完成！");
}
